package com.majun.web;

import com.majun.bean.User;
import com.majun.common.JsonResult;
import com.majun.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

/**
 * @ClassName cn.saytime.web.UserController
 * @Description
 * @date 2017-07-04 22:46:14
 */
@Api(value = "UserController|一个用来测试swagger注解的控制器", tags = { "用户访问接口" })
@RequestMapping(value = "/user")
@RestController
public class UserController {

    @Autowired
    private UserService userService;

    /**
     * 登录
     * api :localhost:8080/user/login
     * @return
     */

//    @ApiImplicitParams({
//        @ApiImplicitParam(name="name",value="姓名",required=true,paramType="form",dataType="String"),
//        @ApiImplicitParam(name="age",value="年龄",required=true,paramType="form",dataType="int")
//    })
//    @ApiResponses({
//        @ApiResponse(code = 200, message = "请求成功"),
//        @ApiResponse(code = 400, message = "请求参数没填好"),
//        @ApiResponse(code = 404, message = "请求路径没有或页面跳转路径不存在")
//    })
    @ApiOperation(value="登录",notes="这是一个登录接口的说明")
    @ResponseBody
    @PostMapping("/login")
    public JsonResult login(@RequestParam String name, @RequestParam Integer age){
        JsonResult jsonResult = new JsonResult();
        User userById = userService.getUserByName(name);
        jsonResult.setResult(userById);
        //...
        return jsonResult;
    }

    /**
     * 通过id获取用户信息
     * api :localhost:8080/user/login
     * @return
     */
    @ApiOperation(value = "通过id获取用户信息", notes="返回用户信息")
    @ResponseBody
    @RequestMapping(value = "info", method = RequestMethod.GET)
    public ResponseEntity<JsonResult> getUserById(@RequestParam(value = "id") Integer id) {
        JsonResult r = new JsonResult();
        try {
            User user = userService.getUserById(id);
            r.setResult(user);
            r.setStatus("ok");
        } catch (Exception e) {
            r.setResult(e.getClass().getName() + ":" + e.getMessage());
            r.setStatus("error");
            e.printStackTrace();
        }
        return ResponseEntity.ok(r);
    }

    /**
     * 添加用户
     *
     * @param user
     * @return
     */
    @ApiImplicitParams(
        @ApiImplicitParam(name="user",value = "majun参数名称===",required = true)
    )
    @ApiOperation(value = "新增用户", notes="返回新增的用户信息")
    @ResponseBody
    @RequestMapping(value = "add", method = RequestMethod.POST)
    public ResponseEntity<JsonResult> add(@RequestBody User user) {
        JsonResult jsonResult = new JsonResult();
        try {
            int orderId = userService.add(user);
            if (orderId < 0) {
                jsonResult.setResult(orderId);
                jsonResult.setStatus("fail");
            } else {
                jsonResult.setResult(orderId);
                jsonResult.setStatus("ok");
            }
        } catch (Exception e) {
            jsonResult.setResult(e.getClass().getName() + ":" + e.getMessage());
            jsonResult.setStatus("error");

            e.printStackTrace();
        }
        return ResponseEntity.ok(jsonResult);
    }

    /**
     * 通过ID删除用户
     * api :localhost:8099/users/2
     * @param id
     * @return
     */
    @ApiOperation(value = "通过id删除用户信息", notes="返回删除状态1 成功 0 失败")
    @RequestMapping(value = "user/{id}", method = RequestMethod.DELETE)
    @ResponseBody
    public ResponseEntity<JsonResult> delete(@PathVariable(value = "id") Integer id) {
        JsonResult jsonResult = new JsonResult();
        try {
            int ret = userService.delete(id);
            if (ret < 0) {
                jsonResult.setResult(ret);
                jsonResult.setStatus("fail");
            } else {
                jsonResult.setResult(ret);
                jsonResult.setStatus("ok");
            }
        } catch (Exception e) {
            jsonResult.setResult(e.getClass().getName() + ":" + e.getMessage());
            jsonResult.setStatus("error");

            e.printStackTrace();
        }
        return ResponseEntity.ok(jsonResult);
    }

    /**
     * 根据id修改用户信息
     *
     * @param user
     * @return
     */

    @ApiImplicitParams(
        @ApiImplicitParam(name="user",value = "update参数名称",required = true,example = "update参数值")
    )
    @ApiOperation(value = "修改用户", notes="返回新增的用户信息")
    @RequestMapping(value = "/update", method = RequestMethod.PUT)
    @ResponseBody
    public ResponseEntity<JsonResult> update(@RequestBody User user) {
        JsonResult jsonResult = new JsonResult();
        try {
            int ret = userService.update(user);
            if (ret < 0) {
                jsonResult.setResult(ret);
                jsonResult.setStatus("fail");
            } else {
                jsonResult.setResult(ret);
                jsonResult.setStatus("ok");
            }
        } catch (Exception e) {
            jsonResult.setResult(e.getClass().getName() + ":" + e.getMessage());
            jsonResult.setStatus("error");

            e.printStackTrace();
        }
        return ResponseEntity.ok(jsonResult);
    }

}
